package cst.a810lw.zyfypt_810lw.Model;

import android.content.SharedPreferences;

import java.util.List;

import cst.a810lw.zyfypt_810lw.Bean.ArticleBean;
import cst.a810lw.zyfypt_810lw.Bean.LoginBean;
import cst.a810lw.zyfypt_810lw.Bean.ProjectBean;
import cst.a810lw.zyfypt_810lw.Bean.SVideoBean;
import cst.a810lw.zyfypt_810lw.Bean.TcaseBean;
import cst.a810lw.zyfypt_810lw.Bean.VideoBean;
import cst.a810lw.zyfypt_810lw.Bean.twareBean;
import cst.a810lw.zyfypt_810lw.Iface.AmicoolIface;
import cst.a810lw.zyfypt_810lw.Listener.AmicoolListener;
import cst.a810lw.zyfypt_810lw.Service.ResService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;


public class ResModel<T> implements AmicoolIface {
    private Retrofit retrofit;
    private ResService service;
    private SharedPreferences sp;
    private static final String Base_URL="http://amicool.neusoft.edu.cn/";
    //private String SESSION_ID;
    //private static  final  String SESSION_ID="nkgfc8ag6jq69e2uepq8u2h242";
    private static  final  String SESSION_ID="tlb40fscfkka9q4eaatkvtajk2";


    public ResModel(String type, String s_id) {
        if(type.equals("gson")){
            retrofit=new Retrofit.Builder().baseUrl(Base_URL).addConverterFactory(GsonConverterFactory.create()).build();
        }
        else{
            retrofit=new Retrofit.Builder().baseUrl(Base_URL).addConverterFactory(ScalarsConverterFactory.create()).build();
        }
        service=retrofit.create(ResService.class);
        //SESSION_ID=s_id;
    }
    public ResModel(String type) {
        if(type.equals("gson")){
            retrofit=new Retrofit.Builder().baseUrl(Base_URL).addConverterFactory(GsonConverterFactory.create()).build();
        }
        else{
            retrofit=new Retrofit.Builder().baseUrl(Base_URL).addConverterFactory(ScalarsConverterFactory.create()).build();
        }
        service=retrofit.create(ResService.class);
        //SESSION_ID="nkgfc8ag6jq69e2uepq8u2h242";
    }

    @Override
    public void login(String User, String PassWord, final AmicoolListener listener) {
        Call<LoginBean> call=service.login(User,PassWord);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void reg(String User, String PassWord, String Tel, int roleid, String realname, String Email, final AmicoolListener listener) {
        Call<String> call=service.reg(User,PassWord,Tel,roleid,realname,Email);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }



    @Override
    public void getList(String mod, int page, final AmicoolListener listener) {
        switch(mod){
            case "video":
                Call<List<VideoBean>>call =service.getVideoList(mod,page,SESSION_ID);
                call.enqueue(new Callback<List<VideoBean>>() {
                    @Override
                    public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                        listener.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;
            case "article":
                Call<List<ArticleBean>> call_a=service.getArticleList(mod,page,SESSION_ID);
                call_a.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        listener.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;
            case "tware":
                Call<List<twareBean>> call_p=service.getTwareList(mod,page,SESSION_ID);
                call_p.enqueue(new Callback<List<twareBean>>() {
                    @Override
                    public void onResponse(Call<List<twareBean>> call, Response<List<twareBean>> response) {
                        listener.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<twareBean>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;
            case"svideo":
                Call<List<SVideoBean>>call_s=service.getSVideoList(mod,page,SESSION_ID);
                call_s.enqueue(new Callback<List<SVideoBean>>() {
                    @Override
                    public void onResponse(Call<List<SVideoBean>> call, Response<List<SVideoBean>> response) {
                        listener.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<SVideoBean>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });

                break;
            case"tcase":
                Call<List<TcaseBean>>call_tcase=service.getTcaseList(mod,page,SESSION_ID);
                call_tcase.enqueue(new Callback<List<TcaseBean>>() {
                    @Override
                    public void onResponse(Call<List<TcaseBean>> call, Response<List<TcaseBean>> response) {
                        listener.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<TcaseBean>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;
            case"project":
                Call<List<ProjectBean>>call_project=service.getProjectList(mod,page,SESSION_ID);
                call_project.enqueue(new Callback<List<ProjectBean>>() {
                    @Override
                    public void onResponse(Call<List<ProjectBean>> call, Response<List<ProjectBean>> response) {
                        listener.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ProjectBean>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;
            default:
                break;
        }
    }

    @Override
    public void getVideoDetail(String mod, int id,String SessionID, AmicoolListener listener) {

    }
}